package com.nanoso.util;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.springframework.http.HttpStatus;

public class HttpUtil {
	private Log log = LogFactory.getLog(HttpUtil.class);
	private HttpClient httpClient;
	private HttpGet httpGet;

	public void init() {
		httpClient = new DefaultHttpClient();
	}

	public String crawl(String url) {

		StringBuffer buffer = null;
		try {
			httpGet = new HttpGet(url);
			HttpResponse httpResponse = httpClient.execute(httpGet);

			if (!(HttpStatus.OK.value() == httpResponse.getStatusLine()
					.getStatusCode())) {
				log.error("crawl url[" + url + "] status code error:"
						+ httpResponse.getStatusLine().getStatusCode());
				return null;
			}

			HttpEntity httpEntity = httpResponse.getEntity();
			if (null == httpEntity)
				return null;

			BufferedReader reader = new BufferedReader(new InputStreamReader(
					httpEntity.getContent(), "UTF-8"));

			String line = null;
			if (httpEntity.getContentLength() > 0) {
				buffer = new StringBuffer((int) httpEntity.getContentLength());
				while ((line = reader.readLine()) != null) {
					buffer.append(line);
				}
			}

		} catch (Exception e) {
			log.error("crawl url error", e);
		}

		return buffer.toString();
	}
}
